package com.zhr.tea.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhr.tea.R;
import com.zhr.tea.api.FenLeiApi;
import com.zhr.tea.entity.CarBean;
import com.zhr.tea.entity.FenLeiProduct;
import com.zhr.tea.view.adapter.FenLeiProductAdapter;
import com.zhr.tea.view.adapter.FenleiTuijianAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

import static com.zhr.tea.R.id.fenlei_et;
import static com.zhr.tea.api.CarService.CAR_URL;

public class FenLeiActivity extends AppCompatActivity implements View.OnClickListener {

    private int sortType = 0;
    private String searchKey = "";
    private int beginId = 0;

    private int goodId = 0;
    private EditText fenleiEt;
    private ImageView fenleiHeadIm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fen_lei);
        searchKey = getIntent().getStringExtra("title");
        initView();
        initData();
        addListener();
    }

    private void intentActivity(){
        Intent intent = new Intent(FenLeiActivity.this, ProductActivity.class);
        intent.putExtra("id", goodId);
        startActivity(intent);
    }

    private void addListener() {
        xrecy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                data.clear();
                beginId = 0;
                initData();
            }

            @Override
            public void onLoadMore() {
                beginId += 20;
                initData();
            }
        });

        adapter.setOnItemClickListener(new FenLeiProductAdapter.XRecyClickListener() {
            @Override
            public void onItemClick(int title) {
                goodId = title;
                intentActivity();

            }
        });

        carAdapter.setOnItemClickListener(new FenleiTuijianAdapter.RecyClickListener() {
            @Override
            public void onItemClick(int title) {
                goodId = title;
                intentActivity();
            }
        });
    }

    private void initData() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject json = new JSONObject();
        json.put("beginId", beginId + "");
        json.put("loadSize", 20);
        json.put("sortType", sortType + "");
        json.put("searchKey", searchKey);
        params.applicationJson(json);
        HttpRequest.post(FenLeiApi.FENLEI_URL, params, new BaseHttpRequestCallback<String>() {
            @Override
            protected void onSuccess(String s) {
                super.onSuccess(s);
                FenLeiProduct product = JSONObject.parseObject(s, FenLeiProduct.class);
                data.addAll(product.getContent().getGoodsList());
                if (data.size() != 0) {
                    adapter.notifyDataSetChanged();
                } else {
                    liner.setVisibility(View.GONE);
                    rela.setVisibility(View.VISIBLE);
                    initTuijian();
                }
                xrecy.refreshComplete();
                xrecy.loadMoreComplete();
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });

    }

    private void initTuijian() {

        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("beginId", "0");
        obj.put("loadSize", 20);
        obj.put("typeIds", "");
        obj.put("goodsIds", "");

        params.applicationJson(obj);
        HttpRequest.post(CAR_URL, params, new BaseHttpRequestCallback<CarBean>() {
            @Override
            protected void onSuccess(CarBean carBean) {
                super.onSuccess(carBean);
                carData.addAll(carBean.getContent());
                carAdapter.notifyDataSetChanged();
            }
        });
    }

    private LinearLayout liner;
    private RelativeLayout rela;

    private List<CarBean.ContentBean> carData = new ArrayList<>();

    private List<FenLeiProduct.ContentBean.GoodsListBean> data;

    private XRecyclerView xrecy;

    private RecyclerView recy;

    private FenLeiProductAdapter adapter;
    private FenleiTuijianAdapter carAdapter;

    private TabLayout tab;


    private void initView() {
        liner = (LinearLayout) findViewById(R.id.fenlei_linerlayout_visible);
        rela = (RelativeLayout) findViewById(R.id.fenlei_relative_visible);
        fenleiEt = (EditText) findViewById(fenlei_et);
        fenleiEt.setOnClickListener(this);
        fenleiEt.setText(searchKey);
        fenleiHeadIm = (ImageView) findViewById(R.id.fenlei_head_im);
        fenleiHeadIm.setOnClickListener(this);
        data = new ArrayList<>();
        settab();
        xrecy = (XRecyclerView) findViewById(R.id.fenlei_xrecy);
        recy = (RecyclerView) findViewById(R.id.fenlei_activity_recy);
        RecyclerView.LayoutManager manager = new GridLayoutManager(this, 2);
        xrecy.setLayoutManager(manager);
        adapter = new FenLeiProductAdapter(data, this);
        xrecy.setAdapter(adapter);

        RecyclerView.LayoutManager manager2 = new GridLayoutManager(this,2);
        recy.setLayoutManager(manager2);
        carAdapter = new FenleiTuijianAdapter(this, carData);
        recy.setAdapter(carAdapter);

    }


    /***
     * tabLayout相关
     */
    private void settab() {
        tab = (TabLayout) findViewById(R.id.fenlei_tab);
        tab.addTab(tab.newTab().setText("综合"));
        tab.addTab(tab.newTab().setText("新品"));
        tab.addTab(tab.newTab().setText("销量"));
        tab.addTab(tab.newTab().setText("价格"));
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                data.clear();
                sortType = tab.getPosition();
                initData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fenlei_head_im:
                this.finish();
                break;
            case R.id.fenlei_et:
                Intent intent = new Intent(FenLeiActivity.this, SearchActivity.class);
                startActivity(intent);
                break;

        }
    }
}

